#include <ros/ros.h>
#include <geometry_msgs/PoseStamped.h>
#include <mavros_msgs/CommandBool.h>
#include <mavros_msgs/SetMode.h>
#include <mavros_msgs/State.h>
static geometry_msgs::PoseStamped local_pose;
static mavros_msgs::State Current_state;
void state_cb(const mavros_msgs::State::ConstPtr& msg)
    {
        Current_state = *msg;}
void pose_cb(const geometry_msgs::PoseStamped::ConstPtr& msg)
    {
        local_pose = *msg;}       
class offboard
{
int mode_state=1;
int point=0;
private:
    ros::NodeHandle nh;
    friend void state_cb(const mavros_msgs::State::ConstPtr& msg);
  //  mavros_msgs::State Current_state;
    friend void pose_cb(const geometry_msgs::PoseStamped::ConstPtr& msg);

    ros::Subscriber state_sub;
    ros::Subscriber plane_local_pos_sub ;

    ros::Publisher target_pose;
    ros::ServiceClient arming_client;
    ros::ServiceClient set_mode_client;
    geometry_msgs::PoseStamped pose;
    mavros_msgs::SetMode offb_set_mode;
    mavros_msgs::CommandBool arm_cmd;
    ros::Time last_request;
    ros::Rate *rate;
public:
    
    offboard(/* args */);
    ~offboard();
    void debug();
    void confirm_connected()
    {
        while(ros::ok() && !Current_state.connected){
        ros::spinOnce();
        ROS_INFO_ONCE("waiting for connecting");
        rate->sleep();

    }
    ROS_INFO_ONCE("connected successfully");
    }
    void arm();
    void take_off();
    void OffBorad();

    void off_borad();
    void copy_msg(const mavros_msgs::State::ConstPtr &msg){
        Current_state=*msg;
    }
};
void offboard::debug()
{
    pose.pose.position.x = 0;
    pose.pose.position.y = 0;
    pose.pose.position.z = 2;
    for(int i = 100; ros::ok() && i > 0; --i){
        target_pose.publish(pose);
        ros::spinOnce();
        rate->sleep();
    }
}

offboard::offboard(/* args */)
{
    //ros::Time::init();
    rate=new ros::Rate(20.0);
    state_sub = nh.subscribe<mavros_msgs::State>
            ("/iris_0/mavros/state", 10, state_cb);
     target_pose = nh.advertise<geometry_msgs::PoseStamped>
            ("/iris_0/mavros/setpoint_position/local", 10);
     arming_client = nh.serviceClient<mavros_msgs::CommandBool>
            ("/iris_0/mavros/cmd/arming");
     set_mode_client = nh.serviceClient<mavros_msgs::SetMode>
            ("/iris_0/mavros/set_mode");
     plane_local_pos_sub = nh.subscribe<geometry_msgs::PoseStamped>("/mavros/local_position/pose", 1, pose_cb);
    offb_set_mode.request.custom_mode = "AUTO.TAKEOFF";
    // confirm_connected();
    // debug();
     
        arm_cmd.request.value = true;
        last_request = ros::Time::now();
    // take_off();



    
}
void offboard::arm()
{
    while (!Current_state.armed&& ros::ok())
    {
        if((ros::Time::now() - last_request > ros::Duration(5.0)))
        {
            ROS_INFO("arm trying");
            if(arming_client.call(arm_cmd) && arm_cmd.response.success)
                    {
                        ROS_INFO("armed");
                        break;
                    }
        last_request=ros::Time::now();
        }
    }
}
void offboard::OffBorad()
{
    ros::spinOnce();
    offb_set_mode.request.custom_mode="OFFBOARD";
    if (Current_state.mode !="OFFBOARD"&&(ros::Time::now()-last_request>ros::Duration(5.0)))
        {
            ROS_INFO("offboard trying");

            if(set_mode_client.call(offb_set_mode)&&offb_set_mode.response.mode_sent)
            {
                ROS_INFO("Offboard enabled");
            } 
            last_request=ros::Time::now();
        }
}
void offboard::take_off(){
while(ros::ok()&&mode_state){
        if( Current_state.mode != "AUTO.TAKEOFF" &&
            (ros::Time::now() - last_request > ros::Duration(5.0)))
            {
            if( set_mode_client.call(offb_set_mode) &&
                offb_set_mode.response.mode_sent){
                ROS_INFO("TAKEOFF enabled");
            }
            last_request = ros::Time::now();
        } else {
            if( !Current_state.armed &&
                (ros::Time::now() - last_request > ros::Duration(5.0))){
                if( arming_client.call(arm_cmd) &&
                    arm_cmd.response.success){
                    ROS_INFO("Vehicle armed");
                }
                last_request = ros::Time::now();
            }

        }
        if (local_pose.pose.position.z>2)
        {
            offb_set_mode.request.custom_mode = "AUTO.TAKEOFF";
            if(set_mode_client.call(offb_set_mode) &&
                offb_set_mode.response.mode_sent)
                {
                    ROS_INFO("OFFBOARD enbaled");
                    mode_state=0;
                    target_pose.publish(pose);
                    point++;
                }
        }

        
        ros::spinOnce();
        rate->sleep();
    }

}
void offboard::off_borad()
{
    
    while(ros::ok()&&mode_state==0)
    {
        if (!((local_pose.pose.position.x>1.8)&&(local_pose.pose.position.x<2.2))&&point==1)
        {ROS_INFO("point 1");
        pose.pose.position.x = 2;
        pose.pose.position.y = 0;
        pose.pose.position.z = 2;
        target_pose.publish(pose);
        rate->sleep();

        }
    }
}

offboard::~offboard()
{
    delete rate;
}

int main(int argc, char **argv)
{
    ros::init(argc, argv, "offb_node_class");
    offboard off;
    off.confirm_connected();
    off.debug();
    off.arm();
    
    off.OffBorad();
   // off.take_off();
    
    //off.off_borad();
}